package util;

/**
 * 转型操作工具类
 *
 * @author huangyong
 * @since 1.0
 */
public class DegreesUtil {

    private static final  double EARTH_RADIUS = 6378137;//赤道半径(单位m)

    /// 数字经纬度和度分秒经纬度转换 (Digital degree of latitude and longitude and vehicle to latitude and longitude conversion)
    /// </summary>
    /// <param name="digitalLati_Longi">数字经纬度</param>
    /// <return>度分秒经纬度</return>
    public static String ConvertDigitalToDegrees(String digitalLati_Longi)
    {
        double digitalDegree = CastUtil.castDouble(digitalLati_Longi);
        return ConvertDigitalToDegrees(digitalDegree);
    }

    /// <summary>
    /// 数字经纬度和度分秒经纬度转换 (Digital degree of latitude and longitude and vehicle to latitude and longitude conversion)
    /// </summary>
    /// <param name="digitalDegree">数字经纬度</param>
    /// <return>度分秒经纬度</return>
     public static String ConvertDigitalToDegrees(double digitalDegree)
    {
        double num = 60;
        int degree = (int)digitalDegree;
        double tmp = (digitalDegree - degree) * num;
        int minute = (int)tmp;
        //匹配
        java.text.DecimalFormat   df=new   java.text.DecimalFormat("#.#");
        double second = (tmp - minute) * num;
        String degrees = "" + degree + "°" + minute + "′" + df.format(second)+ "″";
        return degrees;
    }

    /// <summary>
    /// 度分秒经纬度(必须含有'°')和数字经纬度转换
    /// </summary>
    /// <param name="digitalDegree">度分秒经纬度</param>
    /// <return>数字经纬度</return>
     public static double ConvertDegreesToDigital(String degrees)
    {
        double num = 60;
        double digitalDegree = 0.0;
        int d = degrees.indexOf('°');           //度的符号对应的 Unicode 代码为：00B0[1]（六十进制），显示为°。
        if (d < 0)
        {
            return digitalDegree;
        }
        String degree = degrees.substring(0, d);
        digitalDegree += CastUtil.castDouble(degree);

        int m = degrees.indexOf('′');           //分的符号对应的 Unicode 代码为：2032[1]（六十进制），显示为′。
        if (m < 0)
        {
            return digitalDegree;
        }
        String minute = degrees.substring(d + 1, m );
        digitalDegree += ((CastUtil.castDouble(minute)) / num);

        int s = degrees.indexOf('″');           //秒的符号对应的 Unicode 代码为：2033[1]（六十进制），显示为″。
        if (s < 0)
        {
            return digitalDegree;
        }
        String second = degrees.substring(m + 1, s );
        digitalDegree += (CastUtil.castDouble(second) / (num * num));

        return digitalDegree;
    }

    /// <summary>
    /// 度分秒经纬度(必须含有'/')和数字经纬度转换
    /// </summary>
    /// <param name="digitalDegree">度分秒经纬度</param>
    /// <param name="cflag">分隔符</param>
    /// <return>数字经纬度</return>
    public static double ConvertDegreesToDigital_default(String degrees)
    {
        char ch = '/';
        return ConvertDegreesToDigital(degrees, ch);
    }

    /// <summary>
    /// 度分秒经纬度和数字经纬度转换
    /// </summary>
    /// <param name="digitalDegree">度分秒经纬度</param>
    /// <param name="cflag">分隔符</param>
    /// <return>数字经纬度</return>
    public static double ConvertDegreesToDigital(String degrees, char cflag)
    {
        double num = 60;
        double digitalDegree = 0.0;
        int d = degrees.indexOf(cflag);
        if (d < 0)
        {
            return digitalDegree;
        }
        String degree = degrees.substring(0, d);
        digitalDegree += CastUtil.castDouble(degree);

        int m = degrees.indexOf(cflag, d + 1);
        if (m < 0)
        {
            return digitalDegree;
        }
        String minute = degrees.substring(d + 1, m - d - 1);
        digitalDegree += ((CastUtil.castDouble(minute)) / num);

        int s =  degrees.length();
        if (s < 0)
        {
            return digitalDegree;
        }
        String second = degrees.substring(m + 1, s - m - 1);
        digitalDegree += (CastUtil.castDouble(second) / (num * num));

        return digitalDegree;
    }

    /**
     * 转化为弧度(rad)
     * */
    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }

    /**
     * 基于余弦定理求两经纬度距离
     * @param lon1 第一点的经度
     * @param lat1 第一点的纬度
     * @param lon2 第二点的经度
     * @param lat2 第二点的纬度
     * @return 返回的距离，单位km
     * */
    public static double LantitudeLongitudeDist(double lon1, double lat1,double lon2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);

        double radLon1 = rad(lon1);
        double radLon2 = rad(lon2);

        if (radLat1 < 0)
            radLat1 = Math.PI / 2 + Math.abs(radLat1);// south
        if (radLat1 > 0)
            radLat1 = Math.PI / 2 - Math.abs(radLat1);// north
        if (radLon1 < 0)
            radLon1 = Math.PI * 2 - Math.abs(radLon1);// west
        if (radLat2 < 0)
            radLat2 = Math.PI / 2 + Math.abs(radLat2);// south
        if (radLat2 > 0)
            radLat2 = Math.PI / 2 - Math.abs(radLat2);// north
        if (radLon2 < 0)
            radLon2 = Math.PI * 2 - Math.abs(radLon2);// west
        double x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1);
        double y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1);
        double z1 = EARTH_RADIUS * Math.cos(radLat1);

        double x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2);
        double y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2);
        double z2 = EARTH_RADIUS * Math.cos(radLat2);

        double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)+ (z1 - z2) * (z1 - z2));
        //余弦定理求夹角
        double theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS));
        double dist = theta * EARTH_RADIUS;
        return dist;
    }

    public static void main(String args[]){

        String s ="117.21754";
        String to =ConvertDigitalToDegrees(s);
        System.out.println(to);

        String degrees="24°30′48″";

        double d=ConvertDegreesToDigital(degrees);
        System.out.println(d);

        double long1 =106.486654;
        double lat1  =29.490295 ;
        double long2 =106.581515;
        double lat2  =29.615467 ;

        double dist =LantitudeLongitudeDist(long1,lat1,long2,lat2);

        System.out.println(dist);
    }

}